package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.bo.AccountManagerBO;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Action Class ExampleXMLAction
 */
public class LoginAction extends Action {

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		String message = "";
		String user = "";
		message = request.getParameter("message");
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		
		AccountManagerBO accountBo = new AccountManagerBO();
			if("kiem tra dang nhap".equals(message)){
				if(accountBo.CheckLogin(userName, password)){
					user = userName;
					if(accountBo.CheckAdmin(userName)){
//						session.setAttribute("admin", "admin");
						session.setAttribute("groupId", "1");
//						user = "admin";
					}
					response.getWriter().write(user);
//					response.getWriter().write((String)session.getAttribute("groupId"));
//					response.setStatus(Integer.valueOf((String)session.getAttribute("groupId")));
					session.setAttribute("userName", userName);
				}
				else response.getWriter().write("error");
			}
		
		return null;
	}
}
